﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Contacts;
using Google.GData.Extensions;

namespace CNdotnet.Ezsocio.ContactService.Email {
    public class EmailGmailService : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "gmail.com"; }
        }

        public string StartUrl {
            get { return "http://www.gmail.com"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion

        #region Methods
        public MailContractCollection GetContracts(string userName, string pwd) {
            ContactsService cs = new ContactsService("ucenter");
            cs.setUserCredentials(userName, pwd);
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            MailContractCollection mc = new MailContractCollection();
            ContactsFeed feed = cs.Query(query);

            foreach (ContactEntry entry in feed.Entries) {               
                foreach (EMail email in entry.Emails) {
                    MailContract m = new MailContract();
                    m.Email = email.Address;
                    m.Name = email.Label;
                    mc.Add(m);
                }
            }
            return mc;
        }
        #endregion
    }
}
